/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import entities.Author;
import entities.Book;
import entities.Client;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name = "authorHelper")
@RequestScoped
public class AuthorHelper {

    private long id;
    private String name;
    private String surname;
    private List<Book> books;
    private List<String> booksId;

    public AuthorHelper() {
        books = new ArrayList<Book>();
    }

    public AuthorHelper(String name, String surname) {
        this.name = name;
        this.surname = surname;
    }

    public AuthorHelper(String name, String surname, List<Book> books) {
        this.name = name;
        this.surname = surname;
        this.books = books;
    }

    public AuthorHelper(Author a) {
        this.id = a.getId();
        this.name = a.getName();
        this.surname = a.getSurname();
        this.books = (List<Book>) a.getBooks();
    }

    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public List<String> getBooksId() {
        return booksId;
    }

    public void setBooksId(List<String> booksId) {
        this.booksId = booksId;
    }

    public String create() {
        AuthorDAO authorDao = new AuthorDAO();

        Author author = new Author(name, surname);
        authorDao.create(author);
       // SessionManager sm = new SessionManager();

        SessionManager.getInstance().setRequestItem("messageBean", new MessageBean("Dodano nowego autora"));
        return "authors";
    }

    public String update() {
        System.out.println("wywoluje update na AuthorHelper z id=" + id + " i surname = " + surname);
        BookDAO bookDao = new BookDAO();
        AuthorDAO authorDao = new AuthorDAO();
        Author author = authorDao.findAuthor(id);
        List<Book> booksAuthors = new ArrayList<Book>();
        for (String s : booksId) {
            long bookId = extractId(s);
            Book b = bookDao.findBook(bookId);
            booksAuthors.add(b);
        }

        author.setBooks(booksAuthors);
        author.setName(name);
        author.setSurname(surname);

        try {
            author.update();
        } catch (Exception e) {
            System.out.println("BLAD PODCZAS UPDATOWANIA AUTHORA, " + e.getMessage());
        }
        return "authors";
    }

    private long extractId(String s) {
        int start = s.indexOf("id") + 3;
        int end = s.indexOf("]");
        String id = s.substring(start, end);
        return Long.parseLong(id);
    }
}
